package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Selection;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.ActivityMyBean;
import com.cljz.makepassenger.present.activity.MineActivityP;
import com.cljz.makepassenger.ui.adapter.MineActivityAdapter;
import com.cljz.makepassenger.utils.L;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.widget.ClearEditText;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/5/2.
 */

public class MineActivityActivity extends BaseActivity<MineActivityP> implements XRecyclerView.LoadingListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.cet_mine_activity_search)
    ClearEditText cetMineActivitySearch;
    @BindView(R.id.xrv_mine_activity)
    XRecyclerView xrvMineActivity;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.iv_title_right1)
    ImageView ivTitleRight1;
    @BindView(R.id.fl_title_right1)
    FrameLayout flTitleRight1;
    @BindView(R.id.tv_empty)
    TextView tvEmpty;
    @BindView(R.id.btn_empty)
    Button btnEmpty;
    @BindView(R.id.rl_empty)
    RelativeLayout rlEmpty;
    @BindView(R.id.rl_activity_search)
    RelativeLayout rlActivitySearch;
    private MineActivityAdapter mineActivityAdapter;
    private List<ActivityMyBean.DataBean> activityMyList;
    private String key = "";
    private boolean isLoadMore = false;
    private int page = 1;

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("我的活动");
        tvEmpty.setText("您当前还没有参加的活动");
        rlEmpty.setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xrvMineActivity.setLayoutManager(linearLayoutManager);
        mineActivityAdapter = new MineActivityAdapter(this, activityMyList);
        xrvMineActivity.setAdapter(mineActivityAdapter);
        xrvMineActivity.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);
        xrvMineActivity.setPullRefreshEnabled(false);
        xrvMineActivity.setLoadingMoreEnabled(true);
        getP().activityMy(page, key);
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(rlActivitySearch)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(cetMineActivitySearch, 0);
                    }
                });
        RxView.clicks(btnEmpty)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .putInt("type", EditorInvitedActivity.EDITOR_INVITE)
                                .to(EditorInvitedActivity.class)
                                .launch();
                    }
                });
        xrvMineActivity.setLoadingListener(this);
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop(MineActivityActivity.this);
                    }
                });
        RxTextView.textChanges(cetMineActivitySearch)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        Selection.setSelection(cetMineActivitySearch.getText(), charSequence.length());
                        xrvMineActivity.setNoMore(false);
                        if (charSequence.length() == 0) {
                            key = "";
                        } else {
                            key = charSequence.toString().trim();
                        }
                        L.e("1_net", key);
                        page = 1;
                        getP().activityMy(page, key);
                    }
                });

        mineActivityAdapter.setOnItemClickListener(new MineActivityAdapter.OnItemClickListener() {
            @Override
            public void onClick(ActivityMyBean.DataBean dataBean) {
                Router.newIntent((Activity) App.getInstance()).to(InvitationLetterActivity.class).putInt("type", InvitationLetterActivity.ACTIVITY).putString("id", dataBean.getActivity_id()).launch();

            }

            @Override
            public void onLongClick(ActivityMyBean.DataBean dataBean) {

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_activity;
    }

    @Override
    public MineActivityP newP() {
        return new MineActivityP();
    }

    @Override
    public void onRefresh() {
    }

    @Override
    public void onLoadMore() {
        isLoadMore = true;
        page++;
        getP().activityMy(page, key);
    }

    public void getActivityMySucceed(List<ActivityMyBean.DataBean> dataBeanList) {
        dismissLoadingDialog();
        if (isLoadMore) {
            xrvMineActivity.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                xrvMineActivity.setNoMore(true);
            } else {
                mineActivityAdapter.addData(dataBeanList);
            }
            isLoadMore = false;
        } else {
            activityMyList = dataBeanList;
            if (activityMyList.size() == 0) {
                rlEmpty.setVisibility(View.VISIBLE);
                xrvMineActivity.setVisibility(View.GONE);
            } else {
                rlEmpty.setVisibility(View.GONE);
                xrvMineActivity.setVisibility(View.VISIBLE);
                mineActivityAdapter.setData(activityMyList);
                xrvMineActivity.setNoMore(false);
            }
        }
    }

    public void getActivityMyFail(String error) {
        T.getInstance().showShort(error);
    }

}
